require 'net/ssh'
require 'timeout'
require 'logger'

class DeployController < ApplicationController

  def to_deploy
    @app = App.find(params[:appId])
  end

  def to_rollback
    @app = App.find(params[:appId])
  end

  def to_restart
    @app = App.find(params[:appId])
  end

  def to_start
    @app = App.find(params[:appId])
  end

  def to_stop
    @app = App.find(params[:appId])
  end

  def to_status
    @app = App.find(params[:appId])
  end

  def to_view_version
    @app = App.find(params[:appId])
  end

  def deploy
    #验证参数
    if (!params[:svn_address] || params[:svn_address].strip.empty?)
      @svn_address_error = true
    end
    if (!params[:svn_revision] || !(params[:svn_revision] =~ /^\d+$/))
      @svn_revision_error = true
    end
    if (!params[:git_address] || params[:git_address].strip.empty?)
      @git_address_error = true
    end
    if (!params[:git_revision] || params[:git_revision].strip.empty?)
      @git_revision_error = true
    end
    logger.info params[:svn_git]
    logger.info @svn_address_error
    logger.info @svn_revision_error
    logger.info @git_address_error
    logger.info @git_revision_error

    if(params[:svn_git] == "svn")
      if (@svn_address_error || @svn_revision_error)
        @app = App.find(params[:appId])
        render :to_deploy
        return
      end
    else
      if (@git_address_error || @git_revision_error)
        @app = App.find(params[:appId])
        render :to_deploy
        return
      end
    end


    #验证部署是否合规
    if invalid?(params)
      render :to_deploy
      return
    end

    #初始化deployHistory
    app = App.find(params[:appId])
    deployHistory = DeployHistory.new()
    deployHistory.app_id = app.id
    deployHistory.app_name = app.name
    deployHistory.deploy_type = 'deploy'
    deployHistory.env = params[:env]
    deployHistory.svn_address = params[:svn_address]
    deployHistory.svn_revision = params[:svn_revision]
    deployHistory.svn_git = params[:svn_git]
    deployHistory.git_address = params[:git_address]
    deployHistory.git_revision = params[:git_revision]
    deployHistory.start_time = Time.new
    deployHistory.username = current_user.username
    deployHistory.status = 0
    deployHistory.save()
    DeployToolUtil.add_deploy_task(deployHistory)

    #部署生产环境时，灰度环境做一次跟随部署。
    if(deployHistory.env == DeployToolUtil.env_production and DeployToolUtil.apps_of_huidu.include?(deployHistory.app_name))
      deployHistoryHuidu = deployHistory.dup
      deployHistoryHuidu.env = DeployToolUtil.env_huidu
      deployHistoryHuidu.save()
      DeployToolUtil.add_deploy_task(deployHistoryHuidu)
    end

    redirect_to root_path
  end

  def rollback
    #验证部署是否合规
    if invalid?(params)
      render :to_rollback
      return
    end
    #初始化deployHistory
    app = App.find(params[:appId])
    deployHistory = DeployHistory.new()
    deployHistory.app_id = app.id
    deployHistory.app_name = app.name
    deployHistory.deploy_type = 'rollback'
    deployHistory.env = params[:env]
    deployHistory.svn_address = params[:svn_address]
    deployHistory.svn_revision = params[:svn_revision]
    deployHistory.svn_git = params[:svn_git]
    deployHistory.git_address = params[:git_address]
    deployHistory.git_revision = params[:git_revision]
    deployHistory.start_time = Time.new
    deployHistory.username = current_user.username
    deployHistory.status = 0
    deployHistory.save()
    #启动部署线程
    DeployToolUtil.add_deploy_task(deployHistory)
    redirect_to root_path
  end

  def restart
    #验证部署是否合规
    if invalid?(params)
      render :to_restart
      return
    end
    #初始化deployHistory
    app = App.find(params[:appId])
    deployHistory = DeployHistory.new()
    deployHistory.app_id = app.id
    deployHistory.app_name = app.name
    deployHistory.deploy_type = 'restart'
    deployHistory.env = params[:env]
    deployHistory.svn_address = params[:svn_address]
    deployHistory.svn_revision = params[:svn_revision]
    deployHistory.svn_git = params[:svn_git]
    deployHistory.git_address = params[:git_address]
    deployHistory.git_revision = params[:git_revision]
    deployHistory.start_time = Time.new
    deployHistory.username = current_user.username
    deployHistory.status = 0
    deployHistory.save()
    #启动部署线程
    DeployToolUtil.add_deploy_task(deployHistory)
    redirect_to root_path
  end

  def start
    #验证部署是否合规
    if invalid?(params)
      render :to_start
      return
    end
    #初始化deployHistory
    app = App.find(params[:appId])
    deployHistory = DeployHistory.new()
    deployHistory.app_id = app.id
    deployHistory.app_name = app.name
    deployHistory.deploy_type = 'start'
    deployHistory.env = params[:env]
    deployHistory.svn_address = params[:svn_address]
    deployHistory.svn_revision = params[:svn_revision]
    deployHistory.svn_git = params[:svn_git]
    deployHistory.git_address = params[:git_address]
    deployHistory.git_revision = params[:git_revision]
    deployHistory.start_time = Time.new
    deployHistory.username = current_user.username
    deployHistory.status = 0
    deployHistory.save()
    #启动部署线程
    DeployToolUtil.add_deploy_task(deployHistory)
    redirect_to root_path
  end

  def stop
    #验证部署是否合规
    if invalid?(params)
      render :to_stop
      return
    end
    #初始化deployHistory
    app = App.find(params[:appId])
    deployHistory = DeployHistory.new()
    deployHistory.app_id = app.id
    deployHistory.app_name = app.name
    deployHistory.deploy_type = 'stop'
    deployHistory.env = params[:env]
    deployHistory.svn_address = params[:svn_address]
    deployHistory.svn_revision = params[:svn_revision]
    deployHistory.svn_git = params[:svn_git]
    deployHistory.git_address = params[:git_address]
    deployHistory.git_revision = params[:git_revision]
    deployHistory.start_time = Time.new
    deployHistory.username = current_user.username
    deployHistory.status = 0
    deployHistory.save()
    #启动部署线程
    DeployToolUtil.add_deploy_task(deployHistory)
    redirect_to root_path
  end

  def status
    #查看应用程序状态
    app = App.find(params[:appId])
    deployHistory = DeployHistory.new()
    deployHistory.app_id = app.id
    deployHistory.app_name = app.name
    deployHistory.deploy_type = 'status'
    deployHistory.env = params[:env]
    deployHistory.start_time = Time.new
    deployHistory.username = current_user.username
    deployHistory.status = 0
    deployHistory.save()
    DeployToolUtil.add_deploy_task(deployHistory)
    redirect_to root_path
  end

  def view_version
    #查看应用程序版本
    app = App.find(params[:appId])
    deployHistory = DeployHistory.new()
    deployHistory.app_id = app.id
    deployHistory.app_name = app.name
    deployHistory.deploy_type = 'view_version'
    deployHistory.env = params[:env]
    deployHistory.start_time = Time.new
    deployHistory.username = current_user.username
    deployHistory.status = 0
    deployHistory.save()
    DeployToolUtil.add_deploy_task(deployHistory)
    # if deployHistory.save
    #   render :json => deployHistory
    # end
    redirect_to root_path

  end

  def update_app_deploy_time

  end

  def invalid?(params)
    if params[:env] == DeployToolUtil.env_production \
    and not is_deploy_time? \
    and not current_user.is_admin?
      return true
    end
    return false
  end
end
